package cn.momoshenchi.controller;

import cn.momoshenchi.pojo.Book;
import cn.momoshenchi.util.AssertUtil;
import cn.momoshenchi.util.ResultInfo;
import cn.momoshenchi.pojo.Article;
import cn.momoshenchi.util.PageUtil;
import cn.momoshenchi.service.ArticleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * @version : 1.0
 * @author: momoshenchi
 * @date: 2021/6/7 - 21:31
 */
@RestController
@RequestMapping("/article")
public class ArticleController
{

    @Autowired
    private ArticleService articleService;

    @GetMapping("list")
    @Operation(summary = "查询文章分页")

    public ResultInfo list( PageUtil page)
    {
        IPage<Article> articlePage=new Page<>(page.getPage(), page.getSize());
        articlePage = articleService.page(articlePage);
        return  new ResultInfo(articlePage);
    }


    @GetMapping("{articleId}")
    @Operation(summary = "通过Id查询文章")

    public ResultInfo list(@PathVariable Long articleId)
    {
        Article article = articleService.getById(articleId);
        return  new ResultInfo(article);
    }

    @PostMapping("insert")
    @Operation(summary = "添加文章")
    public ResultInfo insert(@RequestBody Article article) throws IOException
    {
        articleService.insertWithEs(article);
        return  new ResultInfo();
    }

    @PostMapping("insertList")
    @Operation(summary = "批量添加文章")
    public ResultInfo insertList(@RequestBody List<Article> article) throws IOException
    {
        articleService.insertListWithEs(article);
        return  new ResultInfo();
    }

    @PostMapping("update")
    @Operation(summary = "更新文章")
    public ResultInfo update(@RequestBody Article article)
    {
        AssertUtil.isTrue(!articleService.updateById(article),"更新失败");
        return  new ResultInfo();
    }


}
